package com.sptpc.learnspring.global.exception;

import com.sptpc.learnspring.global.response.BusinessStatusCode;
import lombok.Getter;

/**
 * 自定义业务异常
 * 继承RuntimeException，可满足抛出异常时使申明式事务回滚生效
 */
public class BusinessException extends RuntimeException {

    @Getter
    private final BusinessStatusCode resultCode;

    // 只封装错误消息
    public BusinessException(String message) {
        super(message);
        this.resultCode = BusinessStatusCode.FAILURE;
    }

    // 封装返回码和错误消息
    public BusinessException(BusinessStatusCode resultCode) {
        super(resultCode.getMessage());
        this.resultCode = resultCode;
    }

    // 封装错误消息和异常栈
    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        this.resultCode = BusinessStatusCode.FAILURE;
    }

    // 封装返回码、错误消息和异常栈
    public BusinessException(BusinessStatusCode resultCode, Throwable cause) {
        super(cause);
        this.resultCode = resultCode;
    }

    /**
     * 为了获得更好的性能
     *
     */
    @Override
    public Throwable fillInStackTrace() {
        return this;
    }

    public Throwable doFillInStackTrace() {
        return super.fillInStackTrace();
    }

}
